package org.zlpt.contract.dao;

import com.owlike.genson.Genson;
import org.hyperledger.fabric.contract.Context;
import org.hyperledger.fabric.contract.ContractInterface;
import org.hyperledger.fabric.contract.annotation.Contract;
import org.hyperledger.fabric.contract.annotation.Transaction;
import org.hyperledger.fabric.shim.ChaincodeException;
import org.hyperledger.fabric.shim.ChaincodeStub;
import org.zlpt.entity.co.LesseeCompanyCO;


@Contract(
        name = "lesseeCompany"
)
public final class LesseeCompanyContract implements ContractInterface {
    private final Genson genson = new Genson();

    @Transaction(intent = Transaction.TYPE.EVALUATE)
    public boolean LesseeCompanyExists(final Context ctx, String lesseeCompanyJson){
        ChaincodeStub stub = ctx.getStub();
        LesseeCompanyCO lesseeCompany = genson.deserialize(lesseeCompanyJson, LesseeCompanyCO.class);
        String lesseeCompanyJSON = stub.getStringState(lesseeCompany.formKey());

        return (lesseeCompanyJSON != null && !lesseeCompanyJSON.isEmpty());
    }

    @Transaction(intent = Transaction.TYPE.SUBMIT)
    public LesseeCompanyCO CreateLesseeCompany(final Context ctx, String lesseeCompanyJson) {
        ChaincodeStub stub = ctx.getStub();
        if (LesseeCompanyExists(ctx, lesseeCompanyJson)) {
            String errorMessage = String.format("LesseeCompany already exists");
            System.out.println(errorMessage);
            throw new ChaincodeException(errorMessage);
        }
        LesseeCompanyCO lesseeCompany = genson.deserialize(lesseeCompanyJson, LesseeCompanyCO.class);
        stub.putStringState(lesseeCompany.formKey(), lesseeCompanyJson);
        return lesseeCompany;
    }
    @Transaction(intent = Transaction.TYPE.EVALUATE)
    public LesseeCompanyCO ReadLesseeCompany(final Context ctx, String lesseeCompanyId) {
        ChaincodeStub stub = ctx.getStub();
        String keyPrefiexLesseeCompanyId = LesseeCompanyCO.formKey(Long.valueOf(lesseeCompanyId));
        String lesseeCompanyJSON = stub.getStringState(keyPrefiexLesseeCompanyId);
        if (lesseeCompanyJSON == null || lesseeCompanyJSON.isEmpty()) {
            String errorMessage = String.format("LesseeCompany does not exist");
            System.out.println(errorMessage);
            throw new ChaincodeException(errorMessage);
        }

        LesseeCompanyCO lesseeCompany = genson.deserialize(lesseeCompanyJSON, LesseeCompanyCO.class);
        return lesseeCompany;
    }
    @Transaction(intent = Transaction.TYPE.SUBMIT)
    public LesseeCompanyCO UpdateLesseeCompany(final Context ctx, String lesseeCompanyJson) {
        ChaincodeStub stub = ctx.getStub();
        LesseeCompanyCO lesseeCompany = genson.deserialize(lesseeCompanyJson, LesseeCompanyCO.class);

        if (!LesseeCompanyExists(ctx, lesseeCompanyJson)) {
            String errorMessage = String.format("LesseeCompany does not exist");
            System.out.println(errorMessage);
            throw new ChaincodeException(errorMessage);
        }
        String oldLesseeCompanyJson = stub.getStringState(lesseeCompany.formKey());
        LesseeCompanyCO oldLesseeCompany = genson.deserialize(oldLesseeCompanyJson, LesseeCompanyCO.class);
        String sortedJson = genson.serialize(lesseeCompany);
        stub.putStringState(lesseeCompany.formKey(), sortedJson);
        return oldLesseeCompany;
    }
    @Transaction(intent = Transaction.TYPE.SUBMIT)
    public void DeleteLesseeCompany(final Context ctx, String lesseeCompanyId) {
        ChaincodeStub stub = ctx.getStub();

        if (!LesseeCompanyExists(ctx, lesseeCompanyId)) {
            String errorMessage = String.format("LesseeCompany does not exist");
            System.out.println(errorMessage);
            throw new ChaincodeException(errorMessage);
        }

        stub.delState(lesseeCompanyId);
    }
}





